package cn.xqj.org.service.impl;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.service.impl.BaseServiceImpl;
import cn.xqj.basic.util.AjaxResult;
import cn.xqj.basic.util.BaiduAuditUtils;
import cn.xqj.org.domain.Employee;
import cn.xqj.org.domain.Shop;
import cn.xqj.org.domain.ShopAuditLog;
import cn.xqj.org.dto.ShopDto;
import cn.xqj.org.mapper.EmployeeMapper;
import cn.xqj.org.mapper.ShopAuditLogMapper;
import cn.xqj.org.mapper.ShopMapper;
import cn.xqj.org.service.IEmployeeService;
import cn.xqj.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.internet.MimeMessage;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private IEmployeeService employeeServiceImpl;


    @Override
    @Transactional
    public void add(Shop shop) {
        shopMapper.add(shop);
    }


    @Override
    @Transactional
    public void update(Shop shop) {
        shopMapper.update(shop);

    }


    @Override
    @Transactional
    public void settle(Shop shop) {

        // 校验是否有空值
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress()) || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getEmail()) || StringUtils.isEmpty(shop.getEmployee().getPhone())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())){
            throw new BusinessException("数据不能为空");
        }

        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名称违规！AI审核失败！");
        }


        // 校验是否曾经入驻过
        Shop shopTmp = shopMapper.findByInfo(shop);
        if(shopTmp != null){
            throw new BusinessException("已经入驻，请直接登录。忘记密码请联系管理员!!!");
        }

        // 保存数据库
        // 添加employee
        Employee employee = shop.getEmployee();
        employeeServiceImpl.add(employee);

        // 添加shop
        shop.setAdmin_id(employee.getId());
        shopMapper.add(shop);

        // 修改t_employee的shop_id
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);
    }

    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) {
        //1.把店铺的状态设置为4
        Shop shop = shopMapper.findOne(shopAuditLog.getShop_id());

        //2.添加审核日志
        //以后做了登录要去获取登录人信息：现在先写死
        shopAuditLog.setAudit_id(1L);
        shopAuditLog.setState(4);
        shopAuditLogMapper.add(shopAuditLog);

        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //店铺关联的管理员
        Employee manager = employeeMapper.findOne(shop.getAdmin_id());
        //复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //发送方
            helper.setFrom("715184003@qq.com");
            //设置邮件主题
            helper.setSubject("审核失败通知");
            //邮件正文
            helper.setText("<h1>审核失败</h1><br>"+
                    "<span>失败原因："+shopAuditLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8081/#/register'>点击重填资料</a>",true);
            //helper.setTo(manager.getEmail()); - 以后发给店铺入驻时的邮箱
            helper.setTo("449640695@qq.com"); //这里用我自己的邮箱做测试
            //发送消息
            javaMailSender.send(mimeMessage);
            employeeServiceImpl.del(shop.getAdmin_id());
            shopMapper.del(shop.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    @Override
    @Transactional
    public void active(Long id) {
        try {
            Shop one = findOne(id);
            one.setState(3);
            update(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) {
        //1.把店铺的状态设置为2
        Shop shop = shopMapper.findOne(shopAuditLog.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        //以后做了登录要去获取登录人信息：现在先写死
        shopAuditLog.setAudit_id(1L);
        shopAuditLog.setState(2);
        shopAuditLogMapper.add(shopAuditLog);

        //3.发送激活邮件：修改状态 @TODO
        //店铺关联的管理员
        Employee manager = employeeMapper.findOne(shop.getAdmin_id());
        //复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //发送方
            helper.setFrom("715184003@qq.com");
            //设置邮件主题
            helper.setSubject("店铺激活通知");
            //邮件正文
            helper.setText("<h1>店铺激活</h1><br>"+
                    "<span>点击下面的激活链接激活店铺<br>"+
                    "<a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击激活</a>",true);
            //helper.setTo(manager.getEmail()); - 以后发给店铺入驻时的邮箱
            helper.setTo("449640695@qq.com"); //这里用我自己的邮箱做测试
            //发送消息
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
